package com.zzxz.practicaltrainingsys.controller;

import com.alibaba.fastjson.JSON;
import com.zzxz.practicaltrainingsys.entity.Circle;
import com.zzxz.practicaltrainingsys.entity.Circlecomment;
import com.zzxz.practicaltrainingsys.service.CircleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/circle")
public class CircleController {
    @Autowired
    private CircleService circleService;

    // 获取帖子列表
    @GetMapping("/list")
    public List<Circle> getCircleList(@RequestParam String sort) {
        return circleService.getCircleList(sort);
    }

    // 获取单个帖子详情
    @GetMapping("/{circleId}")
    public Circle getCircleDetail(@PathVariable Integer circleId) {
        return circleService.getCircleDetail(circleId);
    }

    // 创建新帖子
    @PostMapping("/create")
    public Integer createCircle(@RequestParam("post") String postJson,
                                @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        Circle circle = JSON.parseObject(postJson, Circle.class);

        if (circle.getCircle_title() == null || circle.getCircle_title().isEmpty()) {
            throw new IllegalArgumentException("标题不能为空");
        }
        return circleService.createCircle(circle, files);
    }

    // 检查用户是否点赞
    @GetMapping("/{circleId}/checkLike")
    public boolean checkLike(@PathVariable Integer circleId, @RequestParam String studentId) {
        Circle circle = circleService.getCircleDetail(circleId);
        return circle.getCircle_good() != null &&
                circle.getCircle_good().contains(studentId);
    }

    // 点赞
    @PostMapping("/likeCircle")
    public Integer likeCircle(@RequestParam String circle_good, @RequestParam Integer circle_id) {
        return circleService.likeCircle(circle_good, circle_id);
    }

    // 添加评论
    @PostMapping("/{circleId}/comment")
    public Integer addComment(@PathVariable Integer circleId, @RequestBody Map<String, String> body) {
        Circlecomment comment = new Circlecomment();
        comment.setCircle_id(circleId);
        comment.setCommentstu_id(body.get("studentId"));
        comment.setComments(body.get("content"));

        // 新增：处理回复评论ID
        String recomment_id = body.get("recommentId");
        if (recomment_id != null && !recomment_id.isEmpty()) {
            comment.setRecomment_id(Integer.valueOf(recomment_id));
        }

        return circleService.addComment(comment);
    }

    // 获取评论
    @GetMapping("/{circleId}/comments")
    public List<Circlecomment> getComments(@PathVariable Integer circleId) {
        return circleService.getCommentsWithReply(circleId);
    }

    //根据身份(circle_role)和账号(circlestu_id)查询本人帖子信息
    @GetMapping("/getCircleByRoleAndId")
    public List<Circle> getCircleByRoleAndId(@RequestParam Integer circlestu_id,@RequestParam String circle_role,@RequestParam String sort) {
        return circleService.getCircleByRoleAndId(circlestu_id, circle_role,sort);
    }

    // 更新帖子
    @PutMapping("/update/{circleId}")
    public ResponseEntity<String> updateCircle(
            @PathVariable Integer circleId,
            @RequestParam("post") String postJson,
            @RequestParam(value = "files", required = false) List<MultipartFile> files,
            @RequestParam(value = "deleted_files", required = false) String deletedFilesJson) {
        try {
            Circle circle = JSON.parseObject(postJson, Circle.class);
            circle.setCircle_id(circleId);

            // 处理删除的文件ID
            if (deletedFilesJson != null && !deletedFilesJson.isEmpty()) {
                List<Integer> deletedFiles = JSON.parseArray(deletedFilesJson, Integer.class);
                circle.setDeleted_files(deletedFiles);
            }

            circleService.updateCircle(circle, files);
            return ResponseEntity.ok("更新成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("更新失败: " + e.getMessage());
        }
    }

    // 删除帖子
    @DeleteMapping("/deleteCircle/{circleId}")
    public ResponseEntity<String> deleteCircle(@PathVariable Integer circleId) {
        try {
            circleService.deleteCircle(circleId);
            return ResponseEntity.ok("删除成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("删除失败");
        }
    }


    //获取点赞用户
    @GetMapping("/{circleId}/likedUsers")
    public List<Map<String, String>> getLikedUsers(@PathVariable Integer circleId) {
        Circle circle = circleService.getCircleDetail(circleId);
        if (circle == null || circle.getCircle_good() == null) {
            return new ArrayList<>();
        }
        return circleService.getLikedUsers(circle.getCircle_good());
    }

    //删除评论
    @DeleteMapping("/deletecomment/{commentId}")
    public ResponseEntity<String> deleteComment(@PathVariable Integer commentId) {
        try {
            circleService.deleteComment(commentId);
            return ResponseEntity.ok("删除成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("删除失败: " + e.getMessage());
        }
    }
}